package algorithm.backtracking.leetcode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author yuisama
 * @date 2023年07月17日 11:19
 * @description 不重复数字全排列
 */
public class OfferII_83_NumPermute {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        boolean[] choices = new boolean[nums.length];
        LinkedList<Integer> state = new LinkedList<>();
        permuteHelper(res,state,choices,nums);
        return res;
    }
    private void permuteHelper(List<List<Integer>> res, LinkedList<Integer> state, boolean[] choices, int[] nums) {
        if (state.size() == nums.length) {
            // 已经找到一个可行解
            res.add(new ArrayList<>(state));
            return;
        }
        // 枚举所有数字
        for (int i = 0; i < nums.length; i++) {
            if (!choices[i]) {
                // 做出选择
                state.add(nums[i]);
                choices[i] = true;
                // 以当前数字为根，继续向下枚举其他数字
                permuteHelper(res,state,choices,nums);
                // 撤销选择
                state.removeLast();
                choices[i] = false;
            }
        }
    }
}
